#!/bin/bash

install() {
    yes | cp  ./ssf/cfssl /usr/bin/
    yes | cp ./ssf/cfssljson /usr/bin/

    cd ./tmp/
    #generate tls
    cfssl gencert -initca ../ca/ca-csr.json | cfssljson -bare ca
    cfssl gencert -ca=ca.pem  -ca-key=ca-key.pem   -config=../ca/ca-config.json  -profile=default   ../ca/consul-csr.json | cfssljson -bare consul

    kubectl create secret generic consul \
      --from-literal="gossip-encryption-key=$(consul keygen)" \
      --from-file=ca.pem \
      --from-file=consul.pem \
      --from-file=consul-key.pem

    cd ..
    kubectl create configmap consul --from-file=configs/server.json

    kubectl create -f pvc/pvc.yaml
    kubectl create -f services/consul.yaml
    kubectl create -f statefulsets/consul.yaml
    kubectl create -f consul-ingress.yaml
}

reset() {
    kubectl delete -f consul-ingress.yaml
    kubectl delete -f statefulsets/consul.yaml
    kubectl delete -f services/consul.yaml
    kubectl delete -f pvc/pvc.yaml
    kubectl delete configmap consul
    kubectl delete secret consul
}

case "$1" in
    install )
        echo "********install********"
		install
		echo "********install********"
		;;
	reset )
	    echo "********reset********"
		reset
		echo "********reset********"
		;;
	* )
	    echo "****************"
		echo "Usage: $0 {install|reset|}"
		echo "****************"
		exit 1
		;;
esac








